package cn.edu.dgut.sai;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Optional;

/**
 * @author 黎志雄
 * 创建于2019-2-18
 *
 * Jakarta EE 课程
 */
@WebServlet(urlPatterns = {"/hello"})
public class HelloServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request,
                         HttpServletResponse response) throws IOException {

        // 告诉浏览器，响应数据的数据格式与字符编码。否则会乱码。
        response.setContentType("text/html;charset=UTF-8");
        // 构造返回的数据流。下面构建html的文档内容。
        response.getWriter().println("<html>");
        response.getWriter().println("<h1>Hello Servlet</h1>");
        Optional.ofNullable(request.getParameter("user")).ifPresent((user) -> {
            try {
                response.getWriter().println("<h1>你好：" + user + "</h1>");
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
//        response.getWriter().println("<h1>欢迎学习 Jakarta EE 课程，网络空间安全学院。</h1>");
//        response.getWriter().println("</br>");
//        response.getWriter().println("session=" + request.getSession(true).getId());
        String requestUrl = request.getRequestURL().toString();//得到请求的URL地址
        String requestUri = request.getRequestURI();//得到请求的资源
        String queryString = request.getQueryString();//得到请求的URL地址中附带的参数
        String remoteAddr = request.getRemoteAddr();//得到来访者的IP地址
        String remoteHost = request.getRemoteHost();
        int remotePort = request.getRemotePort();
        String remoteUser = request.getRemoteUser();
        String method = request.getMethod();//得到请求URL地址时使用的方法
        String pathInfo = request.getPathInfo();
        String localAddr = request.getLocalAddr();//获取WEB服务器的IP地址
        String localName = request.getLocalName();//获取WEB服务器的主机名
        response.getWriter().println("</html>");
        response.getWriter().write ("获取到的客户机信息如下：");
        response.getWriter().write("<hr/>");
        response.getWriter().write("请求的URL地址："+requestUrl);response.getWriter().write("<br/>");
        response.getWriter().write("请求的资源："+requestUri);
        response.getWriter().write("<br/>");
        response.getWriter().write("请求的URL地址中附带的参数："+queryString);
        response.getWriter().write("<br/>");
        response.getWriter().write("来访者的IP地址："+remoteAddr);
        response.getWriter().write("<br/>");
        response.getWriter().write("来访者的主机名："+remoteHost);
        response.getWriter().write("<br/>");
        response.getWriter().write("使用的端口号："+remotePort);
        response.getWriter().write("<br/>");
        response.getWriter().write("remoteUser："+remoteUser);
        response.getWriter().write("<br/>");
        response.getWriter().write("请求使用的方法："+method);
        response.getWriter().write("<br/>");
        response.getWriter().write("pathInfo："+pathInfo);
        response.getWriter().write("<br/>");
        response.getWriter().write("localAddr："+localAddr);
        response.getWriter().write("<br/>");
        response.getWriter().write("localName："+localName);
    }


}